<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        #vpicture {
          transition: width 0.5s, height 0.5s; /* 添加过渡效果 */
        }

        .btnstyle{
            width: 45px;
            height: 30px;
        }

        #currentTableId {
            border: 1px solid red;
        }   

      </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">菜品编号</label>
                            <div class="layui-input-inline">
                               <input class="layui-input">
                            </div>
                        </div>


                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn btnstyle" lay-event="add" ><img src="../images/ic_add.png" alt="" style="width: 24px;margin-left: -1px;"> </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn btnstyle" lay-event="delete"> <img src="../images/删除 (2).png" alt="" style="width: 24px;margin-left: -1px;"> </button>
            </div>
        </script>

        <table class="layui-hide"   id="currentTableId"  lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-warm data-count-delete" lay-event="specs"><img src="../images/规格参数.png"  style="width: 24px;margin-left: -1px;margin-top: -3px;"></a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit"><img src="../images/修改 (1).png"  style="width: 24px;margin-left: -1px;margin-top: -3px;"></a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"><img src="../images/删除 (2).png"  style="width: 24px;margin-left: -1px;margin-top: -3px;"></a>
        </script>

        <script type="text/html" id="status-switch">
            <!-- d 表示当前行 -->
            <input type="checkbox" name="status" style="font-size: 10px" disabled="disabled" value="{{=d.varietyPutStatus}}"  lay-skin="switch" lay-filter="demo-templet-status" {{=d.varietyPutStatus == 1 ? "checked" : "" }} >
        </script>


        <script type="text/html" id="v_go_status-switch">
            <!-- d 表示当前行 -->
            <input type="checkbox" name=" v_go_status" style="font-size: 10px" disabled="disabled" value="{{=d.varietyGoStatus}}"   lay-skin="switch" lay-filter="demo-templet-status" {{=d.varietyGoStatus == 1 ? "checked" : "" }} >
        </script>

        <script type="text/html" id="v-pic">
            <img id="vpicture" class=".layer-mask" src="{{'http://26.83.14.212:5501/'+d.varietyPicture}}" ondblclick="bigimg(this)" style="width:70px;height:50px; title:'点击放大'; cursor: pointer;">
        </script>

    </div>  
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            table = layui.table;
        
        
        var requrl="http://localhost:8080/restaurant_web_war_exploded/"   

         //用于格式化数据
        function init(type_speid,type_spename,addprice,isMenu,parentId,enable){
           return {
            "type_speid":type_speid,
            "type_spename":type_spename,
            "addprice":addprice, 	
            "isMenu": isMenu, 
            "parentId":parentId, 	
            "enable":enable
           }
        }   

        var mySpecsData=[]  
        $.ajax({
            type:"get",
            url:requrl+"spec/listSpec",
            success:function (res){
                let list=res.data.list;
                // console.log(list);
                for (let index = 0; index < list.length; index++) {
                     mySpecsData.push(init(list[index].specId,list[index].specName,0,1,list[index].specTypeId,false));
                }          
            }
        }) 
        

        $.ajax({        
            type:"get",
            url:requrl+"specType/listSpecType",
            success:function (res){
                let list=res.data.list;
               for (let index = 0; index < list.length; index++) {
                    mySpecsData.push(init(list[index].specTypeId,list[index].specTypeName,0,0,-1,false))
                }
            }
        })



        bigimg=function (e){
            layer.photos({
                 photos: { "data": [{"src": e.src}]},
                 shade: [0.7,'#fff']
          });   
        }
        //图片查看   
        
        

        
        table.render({
            elem: '#currentTableId',
            url: requrl+'variety/listVariety',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],    
            cols: [[
                {type: "checkbox", width: 80},
                {field: 'varietyId', width: 70, title: 'ID', sort: true},
                {field: 'varietyName', width: 110, title: '菜品名'},
                {field: 'varietySell', title: '出售数量', Width:120},
                {field: 'varietyPrice', width: 100, title: '价格'},
                {templet:'#v_go_status-switch',width: 120, title: '是否支持配送'},
                {templet:'#status-switch', width: 100, title: '是否上架'},
                {field:"varietyDepict",title: '菜品描述',width: 150},
                {field: 'varietyTypeId', width: 100, title: '类型编号'},
                {title: '菜品图片',width: 180,templet:'#v-pic'},
                {title: '操作', minWidth: 170, toolbar: '#currentTableBar', align: "center"}
            ]],
            parseData:function(res){
                // total=res.data.total;
                return{
                    code: 0, //解析接口状态
                    msg: res.message, //解析提示文本
                    count: res.data.total, //解析数据长度
                    data: res.data.list //解析数据列表
                }
            },
            limits: [5, 10, 15, 20],
            limit: 5,
            page: true,
            skin: 'line'
        });

        window.addEventListener('message', function(event) {  
        if (event.data === 'success') {  
            table.reload('currentTableId');
            layer.msg("菜品添加成功!!!",{icon:6,time:1000},function (){
            })
        }  
        }, false); 

        
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加菜品',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'varietyInfo_add.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            // console.log(obj)
        });


        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'worker_edit.html',
                    success:function(layero, index){
                        var body = layer.getChildFrame('body', index);//获取当前弹窗打开页面Dom元素(对其进行数据回显)
                        var iframeWin = layero.find('iframe')[0].contentWindow;//用于获取当前内容窗口的表单(用于渲染)

                        var body = layer.getChildFrame('body', index);//获取当前弹窗打开页面Dom元素(对其进行数据回显)
                        var iframeWin = layero.find('iframe')[0].contentWindow;//用于获取当前内容窗口的表单(用于渲染)

                        body.find("input[name='wid']").val(obj.data.wid);    
                        body.find("input[name='wname']").val(obj.data.wname);
                        //向下找要回显的元素对其进行回显操作
                        body.find("input[name='code']").val(obj.data.code);
                        body.find("input[name='phone']").val(obj.data.phone);//向下找要回显的元素对其进行回显操作
                        // body.find("input[name='job']").val(obj.data.job);
                        body.find("input[name='entryDate']").val(obj.data.entryDate);
                        //文本框回显

                        body.find("input[name='sex'][value='男']").prop("checked",obj.data.sex=='男'?true:false);
                        body.find("input[name='sex'][value='女']").prop("checked",obj.data.sex=='女'?true:false);
                        body.find("input[name='status'][value='0']").prop("checked",obj.data.status==0?true:false);
                        body.find("input[name='status'][value='1']").prop("checked",obj.data.status==1?true:false);
                        iframeWin.layui.form.render("radio");
                        //复选框回显

                            
                        let myop=data.job;
                        var index=0;
                        let options = body.find("select[name='job'] option");//先拿到option所有值
                        for (let i = 0; i <options.length; i++) {
                            let selop=$(options[i]).val(); 
                            if(selop===myop){
                                index=i;
                                break;  
                            }  
                        }

                        $(options[index]).attr("selected",true);//相中相同项
                        iframeWin.layui.form.render("select");//渲染

                    }   
                });
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            }else if(obj.event==='specs'){
                sessionStorage.setItem("vid",obj.data.varietyId);
                var specindex = layer.open({
                    title: '菜品规格',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'varietySpecs_EditPrice.html',
                    success:function(layero, index){
                        var body = layer.getChildFrame('body', index);//获取当前弹窗打开页面Dom元素(对其进行数据回显)
                        var iframeWin = layero.find('iframe')[0].contentWindow;//用于获取当前内容窗口的表单(用于渲染)

                        body.find("input[name='vname']").val(obj.data.varietyName);   
                        body.find("div ul li input[name='vname_show']").val(obj.data.varietyName);     
                        body.find("input[name='vid']").val(obj.data.varietyId);
                        body.find("input[name='vid_show']").val(obj.data.varietyId);
                        body.find("input[name='vprice']").val(obj.data.varietyPrice);
                        body.find("textarea[name='vdepict']").text(obj.data.varietyDepict);
                    }
                });
                window.addEventListener('message', function(event) {  
                if (event.data === 'addsuccess'||event.data==='delsuccess') {  
                      layer.close(specindex);  
                      var bgindex= layui.layer.load(3, {  
                      shade: [1,'#fff']  })//0.1透明度的白色背景  
                       
                      var index;
                      setTimeout(function(){   
                      layer.close(bgindex);  
                        
                      index = layer.open({
                            title: '',
                            type: 2,
                            closeBtn: 0,
                            shade: 1,
                            maxmin:true,
                            shadeClose: true,
                            area: ['100%', '100%'],
                            content: 'success.html',
                        });
                        // 更新元素          
                        },1200); 
                        
                    window.addEventListener('message', function(event) {  
                    if (event.data === 'closesuccess') {  
                           layer.close(index);
                    }}, false);     
                }  
                }, false); 
            }
        });
    });
</script>

</body>
</html>